package com.neusoft.nep.supervise.controller;

import com.neusoft.nep.common.Dialog;
import com.neusoft.nep.common.FileUtils;
import com.neusoft.nep.common.ViewSwitcher;
import com.neusoft.nep.supervise.entity.Submission;
import com.neusoft.nep.common.SessionManager;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;

import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;


public class SelectAQIViewController {
    @FXML
    private ImageView imageView;
    @FXML
    private Button btn_submit;
    @FXML
    private Button btn_next;
    @FXML
    private Button btn_list;
    @FXML
    private RadioButton radioButton1;
    @FXML
    private RadioButton radioButton2;
    @FXML
    private RadioButton radioButton3;
    @FXML
    private RadioButton radioButton4;
    @FXML
    private RadioButton radioButton5;
    @FXML
    private RadioButton radioButton6;
    @FXML
    private TextArea txt_feedback;
    @FXML
    AnchorPane anchorPane;
    private ToggleGroup AQIGroup;


    public void initialize() {
        AQIGroup = new ToggleGroup();
        radioButton1.setToggleGroup(AQIGroup);
        radioButton2.setToggleGroup(AQIGroup);
        radioButton3.setToggleGroup(AQIGroup);
        radioButton4.setToggleGroup(AQIGroup);
        radioButton5.setToggleGroup(AQIGroup);
        radioButton6.setToggleGroup(AQIGroup);

        Image image = new Image("image/graph.png");
        imageView.setImage(image);
        imageView.setSmooth(true);
        imageView.setPreserveRatio(true);

    }

    public void submit() {
        if (AQIGroup.getSelectedToggle() == null) {
            Dialog.showErrorDialog("提交错误", "请选择AQI等级");
            return;
        }

        String AQI = ((RadioButton) AQIGroup.getSelectedToggle()).getText();
        LocalDateTime date = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedTime = date.format(formatter);
        String feedback = txt_feedback.getText();

        // 关键改进：从会话中获取Grid的id，确保正确关联
        String gridId = SessionManager.getCurrentGridId();
        if (gridId == null) {
            Dialog.showErrorDialog("提交错误", "网格信息丢失，请重新选择网格");
            return;
        }

        String submissionId = generateNewId();
        // 使用改进的构造函数，包含gridId
        Submission sub1 = new Submission(submissionId, AQI, feedback, formattedTime, gridId);

        Map<String, Submission> submissionMap = FileUtils.readMapObject("submission.txt");
        submissionMap.put(submissionId, sub1);
        FileUtils.writeMapObject("submission.txt", submissionMap);

        // 关键改进：将提交记录的id关联到当前用户
        String currentUserId = SessionManager.getCurrentUserId();
        if (currentUserId != null) {
            updateUserSubmissionId(currentUserId, submissionId);
        }

        Dialog.showDialog("公共监督员", "提交成功!提交时间为:" + formattedTime);

        // 清理会话数据
        SessionManager.clearCurrentGridId();
    }
    private void updateUserSubmissionId(String userId, String submissionId) {
        Map<String, com.neusoft.nep.supervise.entity.User> userMap = FileUtils.readMapObject("user.txt");
        com.neusoft.nep.supervise.entity.User user = userMap.get(userId);
        if (user != null) {
            user.setSubmissionId(submissionId);
            userMap.put(userId, user);
            FileUtils.writeMapObject("user.txt", userMap);
        }
    }

    public void next() {
        ViewSwitcher.switchView(btn_next, "../supervise/view/SelectGridView.fxml");
    }

    public void list() {
        ViewSwitcher.switchView(btn_list, "../supervise/view/FeedbackListView.fxml");
    }

    private String generateNewId() {
        Map<String, Submission> submissionMap = FileUtils.readMapObject("submission.txt");
        if (submissionMap.isEmpty()) {
            return "1"; // 如果文件为空，id从1开始
        }
        int maxId = 0;
        for (String key : submissionMap.keySet()) {
            try {
                int currentId = Integer.parseInt(key);
                if (currentId > maxId) {
                    maxId = currentId;
                }
            } catch (NumberFormatException e) {
                // 如果键不是数字，忽略
            }
        }
        return String.valueOf(maxId + 1);
    }
}

